package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.WinService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

@Controller
@RequestMapping("/win")

public class WinAction  implements Serializable{
	private static final Log logger = LogFactory.getLog(WinAction.class);

	@Autowired
	private WinService winService;

	/** 我的邀请码
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/myWinList")
	public void getMyWinList(HttpServletRequest request, HttpServletResponse response){
		Integer current = 1;
		Integer pageSize = 1000;
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");   // 用户id
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);   //当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);     //页大小
		
		// 用户id为空
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
														Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
														Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/win/myWinList 传入参数为空-----userId");
			return;
		}
		
		try {
			if(currentStr != null && !("".equals(currentStr))){
				current = Integer.parseInt(currentStr);
			}
			if(pageSizeStr != null && !("".equals(pageSizeStr))){
				pageSize = Integer.parseInt(pageSizeStr);
			}
			
			// 根据用户id获取已获得奖励列表
			Map<String, Object> jsonMap = winService.getMyWinList(userId, current, pageSize);
			
			String result = CommonUtil.setResultStringCn(jsonMap, Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE,"");
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("/win/myWinList 获取已获得奖励列表 异常：" + e.getCause());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
														Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
														Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
	
}
